#include <mutex>

#include "role.h"

namespace Elements
{

Role::Role(const Role& other)
    : Element(other)
    , m_caption(other.m_caption)
    , m_description(other.m_description)
    , m_basePermissions(other.m_basePermissions)
{
}

bool Role::isSame(Element* const other) const
{
    const auto otherElement = dynamic_cast<Role const*>(other);
    if (nullptr == otherElement)
        return false;

    return bool(
        m_id == otherElement->m_id
        && m_caption == otherElement->m_caption
        && m_description == otherElement->m_description
        && m_basePermissions == otherElement->m_basePermissions
    );
}

Common::Json Role::toJson() const
{
    std::shared_lock locker(m_lock);
    Common::Json result;
    result["id"] = m_id;
    result["elementType"] = unsigned(elementType());
    result["caption"] = m_caption;
    result["description"] = m_description;
    result["basePermissions"] = unsigned(m_basePermissions);
    return result;
}

void Role::fromJson(const Common::Json& json)
{
    std::shared_lock locker(m_lock);
    m_id = json["id"].get<unsigned>();
    m_caption = json["caption"].get<std::string>();
    m_description = json["description"].get<std::string>();
    m_basePermissions = BasePermissions(json["basePermissions"].get<unsigned>());
}

Element* Role::clone() const
{
    return new Role(*this);
}

ElementType Role::elementType() const
{
    return ElementType::Role;
}

std::string Role::caption() const
{
    std::shared_lock locker(m_lock);
    return m_caption;
}

void Role::setCaption(const std::string& value)
{
    std::unique_lock locker(m_lock);
    m_caption = value;
}

std::string Role::description() const
{
    std::shared_lock locker(m_lock);
    return m_description;
}

void Role::setDescription(const std::string& value)
{
    std::unique_lock locker(m_lock);
    m_description = value;
}

BasePermissions Role::basePermissions() const
{
    std::shared_lock locker(m_lock);
    return m_basePermissions;
}

void Role::setBasePermissions(const BasePermissions& value)
{
    std::unique_lock locker(m_lock);
    m_basePermissions = value;
}

} // namespace Elements
